home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
By the Book
/
Mac C Primer V2
/
4.4 - GWorld
/
GWorld.c
next >
Wrap
C/C++ Source or Header
|
1991-08-21
|
5KB
|
206 lines
/************************************************************/
/* */
/* GWorld Code from Chapter Four of */
/* */
/* *** The Macintosh Programming Primer *** */
/* */
/* Copyright 1990, Dave Mark */
/* */
/* This program demonstrates specific Mac programming */
/* techniques. */
/* */
/************************************************************/
#include "Picker.h"
#include "QDOffscreen.h"
#define BASE_RES_ID 400
#define NIL_POINTER 0L
#define NIL_STRING "\p"
#define VISIBLE TRUE
#define NO_GOAWAY FALSE
#define MOVE_TO_FRONT (WindowPtr)-1L
#define REMOVE_ALL_EVENTS 0
#define MAX_PIXEL_DEPTH 32
#define WORLD_WIDTH 100
#define WORLD_HEIGHT 100
#define NO_FLAGS 0L
#define QD32TRAP 0xAB03
#define UNIMPL_TRAP 0xA89F
Boolean Is32Bit();
GWorldPtr MakeGWorld();
WindowPtr CreateColorWindow();
main()
{
WindowPtr window;
GWorldPtr world;
Rect worldBounds, windowRect, destRect;
ToolBoxInit();
if ( ! Is32Bit() )
DoAlert( "\pThis machine does not support 32-Bit QuickDraw!" );
else
{
SetRect( &worldBounds, 0, 0, WORLD_HEIGHT, WORLD_WIDTH );
world = MakeGWorld( &worldBounds );
window = CreateColorWindow();
SetRect( &destRect, 0, 0, 4 * WORLD_WIDTH, 4 * WORLD_HEIGHT );
CopyWorldBits( world, window, &destRect );
SetRect( &destRect, 0, 0, 2 * WORLD_WIDTH, 2 * WORLD_HEIGHT );
CopyWorldBits( world, window, &destRect );
SetRect( &destRect, 0, 0, WORLD_WIDTH, WORLD_HEIGHT );
CopyWorldBits( world, window, &destRect );
SetRect( &destRect, 0, 0, WORLD_WIDTH / 2, WORLD_HEIGHT / 2 );
CopyWorldBits( world, window, &destRect );
SetRect( &destRect, 0, 0, WORLD_WIDTH / 4, WORLD_HEIGHT / 4 );
CopyWorldBits( world, window, &destRect );
while ( ! Button() ) ;
}
}
/*********************************** ToolBoxInit */
ToolBoxInit()
{
InitGraf( &thePort );
InitFonts();
FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
InitWindows();
InitMenus();
TEInit();
InitDialogs( NIL_POINTER );
InitCursor();
}
/*********************************** CreateColorWindow */
WindowPtr CreateColorWindow()
{
WindowPtr cWindow;
Rect r;
SetRect( &r, 10, 40, 10 + (4 * WORLD_WIDTH),
40 + (4 * WORLD_HEIGHT) );
cWindow = NewCWindow( NIL_POINTER, &r, "\pColor Test",
VISIBLE, noGrowDocProc, MOVE_TO_FRONT,
NO_GOAWAY, NIL_POINTER );
SetPort( cWindow );
return( cWindow );
}
/*********************************** MakeGWorld */
GWorldPtr MakeGWorld( boundsPtr )
Rect *boundsPtr;
{
GDHandle oldGD;
GWorldPtr oldGW, newWorld;
HSVColor hsvColor;
RGBColor rgbColor;
long i;
Rect r;
QDErr errorCode;
GetGWorld( &oldGW, &oldGD );
errorCode = NewGWorld( &newWorld, MAX_PIXEL_DEPTH,
boundsPtr, NIL_POINTER,
NIL_POINTER, NO_FLAGS );
if ( errorCode != noErr )
{
DoAlert( "\pMy call to NewGWorld died! Bye..." );
ExitToShell();
}
LockPixels( newWorld->portPixMap );
SetGWorld( newWorld, NIL_POINTER );
hsvColor.value = 65535;
hsvColor.saturation = 65535;
for( i=boundsPtr->left; i<=boundsPtr->right; i++ )
{
hsvColor.hue = i * 65535 / ( boundsPtr->right - 1 );
HSV2RGB( &hsvColor, &rgbColor );
RGBForeColor( &rgbColor );
MoveTo( i, boundsPtr->bottom / 2 );
LineTo( i, boundsPtr->bottom );
rgbColor.red = i * 65535 / ( boundsPtr->right - 1 );
rgbColor.green = rgbColor.red;
rgbColor.blue = rgbColor.red;
RGBForeColor( &rgbColor );
MoveTo( i, 0 );
LineTo( i, boundsPtr->bottom / 2 );
}
SetGWorld(oldGW,oldGD);
UnlockPixels( newWorld->portPixMap );
return( newWorld );
}
/*********************************** CopyWorldBits */
CopyWorldBits( world, window, destRectPtr )
GWorldPtr world;
WindowPtr window;
Rect *destRectPtr;
{
RGBColor rgbBlack;
rgbBlack.red = rgbBlack.green = rgbBlack.blue = 0;
RGBForeColor( &rgbBlack );
LockPixels( world->portPixMap );
CopyBits( &world->portPixMap, &thePort->portBits,
&world->portRect, destRectPtr, ditherCopy, 0 );
UnlockPixels( world->portPixMap );
}
/******************************** Is32Bit *********/
Boolean Is32Bit()
{
SysEnvRec mySE;
SysEnvirons( 1, &mySE );
if ( ! mySE.hasColorQD )
return( FALSE );
return( NGetTrapAddress( QD32TRAP, ToolTrap ) !=
NGetTrapAddress( UNIMPL_TRAP, ToolTrap ) );
}
/*********************************** DoAlert */
DoAlert( s )
Str255 s;
{
ParamText( s, NIL_STRING, NIL_STRING, NIL_STRING );
NoteAlert( BASE_RES_ID, NIL_POINTER );
}